package service

import (
	"encoding/json"
	"testing"

	"gitee.com/actor168/gitserver/pkg/config"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
)

type ts struct {
	suite.Suite
	svc GitInterface
}

func (s *ts) SetupSuite() {
	config.Init("/mnt/e/Workspace/Extern/gitserver/config.yaml")
	s.svc = GitRepository
}

func (s *ts) TearDownSuite() {

}

func (s *ts) TestCreateRepo() {
	assert.NotNil(s.T(), s.svc)
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\"}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "CreateRepo",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), resp.Code, 200)
	s.T().Error(resp)
}

func (s *ts) TestCodeBranchs() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"branchName\": \"all\"}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "Codebranches",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	assert.Equal(s.T(), "OK", resp.Message)
	data, _ := json.Marshal(resp.Data)
	s.T().Error(string(data))
}

func (s *ts) TestListBriefBranchs() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\"}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "ListBriefBranchs",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	assert.Equal(s.T(), "OK", resp.Message)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}

func (s *ts) TestSetProtectedbranches() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"branchName\": \"master\", \"isProtect\": true}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "SetProtectedbranches",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 202, resp.Code)
	assert.Equal(s.T(), "Accepted", resp.Message)
	data, _ := json.Marshal(resp.Data)
	s.T().Error(string(data))
}

func (s *ts) TestSetDefaultBranch() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"branchName\": \"master\", \"isProtect\": true}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "SetDefaultBranch",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 202, resp.Code)
	assert.Equal(s.T(), "Accepted", resp.Message)
	data, _ := json.Marshal(resp.Data)
	s.T().Error(string(data))
}

func (s *ts) TestHistoryCommitList() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"branchName\": \"test\", \"pageSize\": 50}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "HistoryCommitList",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	data, _ := json.Marshal(resp.Data)
	s.T().Error(string(data))
}

func (s *ts) TestCodecomparison() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"commitID\": \"de0b7f265690233edb9828d821440c11e63a059d\"}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "Codecomparison",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}

func (s *ts) TestRepoContent() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"branchName\": \"master\", \"subPath\": \"README.md\"}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "RepoContent",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}

func (s *ts) TestRepoContent1() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"13291512066\", \"repoName\": \"ces7\", \"branchName\": \"dev\", \"subPath\": \"\"}",
		Name:        "ces7",
		Code:        "13291512066",
		Operate:     "RepoContent",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}

func (s *ts) TestPullRequestcomparison() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"before\": \"master\", \"after\": \"test\"}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "PullRequestcomparison",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}

func (s *ts) TestIsPullRequestcomparison() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"before\": \"master\", \"after\": \"test\"}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "IsPullRequestcomparison",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}

func (s *ts) TestMergePull() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"pull\": {\"IssueId\":1, \"UserName\": \"admin\", \"HeadRepo\":\"actor168/test\", \"BaseRepo\":\"actor168/test\", \"HeadBranch\": \"test\", \"BaseBranch\": \"master\", \"HasMerged\": false, \"MergeBase\": \"38ca852d42dfe2256427d59da67929a13b35da26\"}, \"pulls\": [], \"mergeStyle\": \"create_merge_commit\"}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "MergePull",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}

func (s *ts) TestSetBranchs() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"defaultBranch\": \"master\", \"protectedBranch\": \"master\"}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "Codesettingbranches",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 202, resp.Code)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}

func (s *ts) TestListPRInfo() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"pr\": {\"HeadRepo\":\"actor168/test\", \"BaseRepo\":\"actor168/test\", \"HeadBranch\": \"test\", \"BaseBranch\": \"master\", \"HasMerged\": false, \"MergeBase\": \"38ca852d42dfe2256427d59da67929a13b35da26\"}}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "ListPRInfo",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}
func (s *ts) TestPRCommits() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"pr\": {\"HeadRepo\":\"actor168/test\", \"BaseRepo\":\"actor168/test\", \"HeadBranch\": \"test\", \"BaseBranch\": \"master\", \"HasMerged\": false, \"MergeBase\": \"\"}}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "PRCommits",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 200, resp.Code)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}

func (s *ts) TestPRFiles() {
	req := &GitReqModel{
		URI:         "/XXX/XX",
		RequestData: "{\"code\": \"actor168\", \"repoName\": \"test\", \"pr\": {\"HeadRepo\":\"actor168/test\", \"BaseRepo\":\"actor168/test\", \"HeadBranch\": \"test\", \"BaseBranch\": \"master\", \"HasMerged\": false, \"MergeBase\": \"\"}}",
		Name:        "test",
		Code:        "actor168",
		Operate:     "PRFiles",
	}
	resp := s.svc.GitOperate(req)
	assert.Equal(s.T(), 202, resp.Code)
	data, _ := json.Marshal(resp)
	s.T().Error(string(data))
}

func TestTestSuite(t *testing.T) {
	suite.Run(t, new(ts))
}
